home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 130_01 / long.doc < prev    next >
Text File  |  1985-03-09  |  5KB  |  152 lines

  1.                    LONG INTEGERS
  2.  
  3.               (c) 1981 by Paul J. Gans
  4.  
  5. /* modified by SP 9/82 -
  6.  
  7.     added functions:  ltou(u, longform) convert low 16 bits to unsigned
  8.               utol(longform, u) convert unsigned to longform
  9. */
  10.  
  11. The Long Integer Package
  12.  
  13.      The Long Integer Package is a collection of functions
  14. designed to provide the users of Leor Zolman's BDS C with
  15. the ability to manipulate long integers.  This package has
  16. been modeled upon the Floating Point Package for BDS C
  17. written by Bob Mathias and works in a similar manner.
  18.  
  19.      The package works with long integers stored as four
  20. consecutive bytes with the least significant byte stored
  21. at the lowest memory address.  The precision maintained is
  22. 31 bits plus sign.  To use the package, all long integers
  23. should be declared as four byte char arrays.  Manipulation
  24. is by pointer to the arrays.  Thus the code
  25.  
  26.         char first[4], second[4], result[4];
  27.  
  28. could define three long integers.
  29.  
  30.      The possible range a long integer longform may
  31. occupy is:
  32.  
  33.           -2147483648 <= longform <= 2147483647
  34.  
  35.  
  36.      Long integers are manipulated via the following
  37. functions:
  38.  
  39.    char *ladd(sum,addend,augend)
  40.    char sum[4], addend[4], augend[4];
  41.       adds addend and augend, stores the 32 bit signed
  42.       result in sum and returns a pointer to sum.
  43.  
  44.    char *lsub(diff,minuend,subtrahend)
  45.    char diff[4], minuend[4], subtrahend[4];
  46.       subtracts subtrahend from minuend and places the
  47.       32 bit signed result in diff.  A pointer to diff
  48.       is returned.
  49.  
  50.    char *lmul(prod,plier,plicand)
  51.    char prod[4], plier[4], plicand[4];
  52.       plier and plicand are multiplied.  The least
  53.       signficant 31 bits of the product along with the
  54.       appropriate sign bit are stored in prod.  A pointer
  55.       to prod is returned.
  56.  
  57.    char *ldiv(quot,dividend,divisior)
  58.    char quot[4], dividend[4], divisor[4];
  59.       dividend is divided by divisor.  The least
  60.       significant 31 bits of the quotient along with the
  61.       proper sign are stored in quot.  A pointer to prod
  62.       is produced.  Note that no indication of overflow
  63.       is produced should it occur.
  64.  
  65.    char *lmod(res,dividend,divisor)
  66.    char res[4], dividend[4], divisor[4];
  67.       dividend is divided by divisor.  The positive 31
  68.       bit remainder is placed in res and a pointer to it
  69.       is returned.
  70.  
  71.    char *lneg(newnum,orignum)
  72.    char newnum[4], orignum[4];
  73.       orignum is negated and placed in newnum.  A pointer
  74.       to newnum is returned.
  75.  
  76.    char *itol(longform,i)
  77.    char longform[4];
  78.    int  i;
  79.       i is converted to long integer form and placed in
  80.       longform to which a pointer is returned.
  81.  
  82. /* note - following function modified from ver 1.0 to fix minor
  83. bug, original didn't place value in caller's 'i',  now needs address
  84. of 'i' passed to it, it will then set i properly.  9/82  SP */
  85.  
  86.    int ltoi(&i,longform)
  87.    char longform[4];
  88.    int  *i;
  89.       the low order 15 bits of longform along with the
  90.       proper sign are placed in i, which value is returned.
  91.  
  92.    char *atol(longform,s)
  93.    char longform[4], *s;
  94.       the '\0' terminated ASCII string s is converted to
  95.       a long integer and stored in longform to which a
  96.       pointer is returned.  The proper format for s is:
  97.       any amount of white space followed by an optional
  98.       sign followed by decimal digits.  The first non-
  99.       digit terminates the scan.  No check is made for
  100.       overflow and improper results will result if too
  101.       many decimal digits are presented.
  102.  
  103. /* note - following modified from ver 1.0 to preserve value held
  104.     in longform. 9/82 SP */
  105.  
  106.    char *ltoa(s,longform)
  107.    char longform[4], *s;
  108.       the long integer longform is converted to a '\0'
  109.       terminated ASCII string prefixed by a minus sign
  110.       if longform is negative.  The string is placed in
  111.       s to which a pointer is returned.  Note that the
  112.       maximum length s may attain is 12, counting 10
  113.       numerical digits, a possible minus sign, and the
  114.       terminating '\0'.
  115.  
  116.  
  117.  
  118. /********************************************************
  119.  
  120.     following added for cnode use, 9/82 SP.
  121.  
  122. */
  123.  
  124.  
  125. char *
  126. utol(longform, u)
  127. char *longform;
  128. unsigned u;
  129.  
  130.     utol converts an unsigned to long form.  A pointer
  131. to the longform is returned.  longform must point to an array
  132. of 4 char.
  133.  
  134.  
  135. ltou(u, longform)
  136. unsigned *u;
  137. char *longform;
  138.  
  139.     ltou returns an unsigned integer containing the
  140. low order 16 bits of precision of the long integer
  141. longform.  The value of this integer is placed in u and
  142. is returned by this function.  It assumes that long is
  143. positive. BE SURE TO PASS THE ADDRESS OF THE UNSIGNED
  144. TO THIS FUNCTION, not the unsigned itself.
  145.  
  146.  
  147. /** end of additions doc **/
  148.  
  149.      The Long Integer Package was written by Paul J. Gans,
  150. Department of Chemistry, New York University, New York,
  151. NY  10003.  Phone (212) 598-2515.  Bug reports welcomed.
  152.